package com.sevennotes.classtable.data.database

import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverter
import androidx.room.TypeConverters
import com.sevennotes.classtable.data.database.daos.*
import com.sevennotes.classtable.model.*
import java.util.*

@Database(
    entities = [
        ClassItem::class,
        StudentItem::class,
        TableName::class,
        ClassRecord::class,
        ClassAmountRecord::class,
        GradePointRecord::class
    ],
    version = 2,
    exportSchema = false
)
@TypeConverters(Converters::class)
abstract class ClassTableDatabase : RoomDatabase() {
    abstract fun classItemDao(): ClassItemDAO
    abstract fun studentItemDao(): StudentItemDAO
    abstract fun tablenamesDao(): TableNamesDAO
    abstract fun classRecordDao(): ClassRecordDAO
    abstract fun classAmountRecordDao(): ClassAmountRecordDAO
    abstract fun gradePointRecordDao(): GradePointRecordDAO
}

class Converters {
    @TypeConverter
    fun fromTimestamp(value: Long?): Date? {
        return value?.let { Date(it) }
    }

    @TypeConverter
    fun dateToTimestamp(date: Date?): Long? {
        return date?.time
    }
}
